<template>
	<view>
		<!-- 标题 -->
		<view class="login-top">
			<!-- logo -->
			<view class="login-top-image">
				<image :src="getStaticFilePath('/login/logo.png')"></image>
			</view>
			<!-- 名字 -->
			<view class="login-top-title">
				飞砂走石
			</view>
			<!-- 平台 -->
			<view class="login-top-title-text">
				客户在线下单采购平台
			</view>
		</view>
		<view class="login-content">
			<view class="login-li">
				<text class="iconfont iconshouji3"></text>
				<input v-model="iphone" type="number" maxlength="11" value="" />
			</view>
			<view class="login-li">
				<text class="iconfont icona-yanzhengma1"></text>
				<input v-model="code" type="number" maxlength="6" value="" />
				<view @click="getCode">
					{{codetext}}
				</view>
			</view>
			<view class="login-li">
				<text class="iconfont iconduanxin1"></text>
				<input v-model="yqm" type="number" maxlength="6" placeholder="选填" value="" />
				<view style="color: #666666;">
					邀请码
				</view>
			</view>
		</view>
		<view class="login-button">
			<button class="login-content-btn" :class="btncolor" @click="submitLogin" type="primary">登录</button>

		</view>

		<view class="atdlfs-main-sty">
			<text>其他登录方式</text>
		</view>
		<view class="" style="width: 100%;margin-bottom: 40rpx;text-align: center; padding: 0 20rpx;">
			<!-- <image @click="getUserProfile()" style="width: 70rpx;height: 70rpx;display: inline-block;"
				:src="getStaticFilePath('/home/wd/WX.png')" mode="widthFix"></image>
			<view class="" style="text-align: center;padding-bottom: 40rpx;">
				微信登录
			</view> -->
			<view class="sjhyjdl-sty">
				<button style="background-color:#39B54A;" type="primary" @tap="goLogin">手机号一键登录</button>
			</view>
		</view>

		<view class="checkbox">
			<checkbox-group @change="checkboxChange">
				<label v-for="option in options" :key="option.value">
					<checkbox :value="option.value" :checked="isChecked(option.value)" />
				</label>
			</checkbox-group>
			我已阅读并同意<text @click="privacy">《使用协议和隐私政策》</text>
		</view>

		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options: [{
					value: '1',
					text: ''
				}, ],
				selectedValues: [], // 初始化选中的值
				checked: true,
				timeOut: 60,
				token: '',
				iphone: '',
				code: '',
				yqm: '',
				codetext: '获取验证码',
				ErpUserTypeText: '',
				cid: uni.getStorageSync('cid'),
				codewx: '',
				sessionKey: '',
				openid: ''

			}
		},
		computed: {
			btncolor() {
				var i = ''
				var c = ''
				if (this.iphone && this.iphone != '') {
					i = this.iphone.trim()
				}
				if (this.code && this.code != '') {
					c = this.code.trim()
				}
				if (i.length == 11 && (c.length == 6 || c.length == 4)) {
					return ''
				} else {
					return 'login-content-btn-active'
				}
			}
		},
		onShow(ops) {
			// let allPages = getCurrentPages(); //获取当前页面栈的实例；
			// console.log(allPages);
			// let lastPages = allPages.length - 1; // 获得倒数第二个元素的索引；
			// console.log(lastPages);
			// let option = allPages[lastPages].options; // 获得上个页面传递的参数；
			// console.log(option);
			// this.yzmdl = option.sjhdl

			// this.init()
			// // console.log(this.cid)
			this.getToken()
			this.ErpUserTypeText = this.ErpUserType
			uni.getSystemInfo({
				success: function(res) {
					var type = false
					if (res.host || res.platform == 'devtools') {} else {
						type = true
					}
					uni.setStorageSync('platform', type);
				}
			});


		},
		onLaunch: function() {
			//只有在基座运行的情况下才可以进行打印看到
			const clientInfo = plus.push.getClientInfo()
			// console.log(clientInfo)
		},
		onLoad(ops) {
			
			
			// else{
			// 	uni.navigateTo({
			// 		url: '/pages/sjhyjdl/index'
			// 	});
			// }

		},
		methods: {
			goLogin() {
				if (this.selectedValues.length == 0) {
					return this.$refs.uToast.show({
						title: '请勾选协议',
						type: 'default',
						icon: false
					})
				}
				uni.navigateTo({
					url: '/pages/sjhyjdl/index'
				});

			},
			//获取手机号
			getPhoneNumberInfo(access_token, openid) {
				// 在得到access_token和openid后，通过callfunction调用云函数
				uniCloud.callFunction({
					name: "getPhoneNumber",
					data: {
						access_token: access_token,
						openid: openid
					}
				}).then(res1 => {
					console.log("获取成功");
					console.log(res1);
					if (res1.result.code == 0) {
						// 获取用户的手机号
						let phoneNumber = res1.result.phoneNumber.phoneNumber;
						console.log(phoneNumber)
						// let hta = this
						uni.request({
							url: this.baseUrl + '/system/autoLogin',
							method: 'POST',
							dataType: 'json',
							data: {
								PERTB005CA03: phoneNumber
							},
							success: (res) => {
								console.log(res);
								if (res.data.code == 200) {
									console.log(res.data.data)
									console.log(this.token)
									var str = JSON.stringify(res.data.data)
									uni.closeAuthView()
									uni.setStorageSync('userinfo', str);
									uni.setStorageSync('token', this.token);
									//成功关闭授权页面


									console.log(uni.getStorageSync('token'))

									// setTimeout(() => {

									// 	uni.switchTab({
									// 		url: '/pages/tabbar/home/index'
									// 	});
									// }, 5000);


								} else {
									uni.showToast({
										title: res.data.message,
										type: 'default',
										icon: false
									})

								}
								// uni.hideLoading();
							},
							fail: (res) => {
								uni.showToast({
									title: '服务器链接超时',
									type: 'default',
									icon: false
								})
								// uni.hideLoading();
							}
						})

					} else {
						uni.showToast({
							title: '手机号获取失败请切换其他登陆方式',
							type: 'default',
							icon: false
						})
					}
				}).catch((err) => {
					console.log(err)

					uni.showToast({
						title: '接口信息错误',
						type: 'default',
						icon: false
					})


				})


			},
			init() {
				if (uni.getStorageSync('userinfo')) {
					var userinfo = JSON.parse(uni.getStorageSync('userinfo'))
					if (userinfo.PERTB005ID != undefined) {
						uni.switchTab({
							url: '../tabbar/home/index'
						});
					}
					
				}
				
				else {
					// uni.navigateTo({
					// 	url: '/pages/sjhyjdl/index'
					// });
				}
			},
			privacy() {
				uni.navigateTo({
					url: '/pages/business/privacy/privacyText?url='+'https://feishazoushi.cn/yszc/client_index.html'
				})
				
				
			},
			checkboxChange(event) {
				this.selectedValues = event.detail.value; // 更新选中的值
				// console.log('选中的值:', this.selectedValues);
			},
			isChecked(value) {
				return this.selectedValues.indexOf(value) !== -1; // 检查选项是否被选中
			},
			// getUserProfile() {
			// 	let that = this //保存当前作用域

			// 	uni.login({ //直接用这个调用微信接口
			// 		"provider": "weixin",
			// 		"onlyAuthorize": true, // 微信登录仅请求授权认证
			// 		success: function(response) { // 用微信登录的话就要去微信开发工具
			// 			console.log(response.code) //这里打印就说明接口调用成功了，然后看message login ：ok
			// 			//微信登录就完了，后面就是获取用户信息
			// 			uni.request({
			// 				url: that.baseUrl + '/system/appGetOpenId/',
			// 				method: 'POST',
			// 				dataType: 'json',
			// 				data: {
			// 					code: response.code,
			// 				},
			// 				success(response) {
			// 					console.log(response)
			// 					if (response.errMsg == 'request:ok') {
			// 						// that.sessionKey = response.data.data.sessionKey
			// 						that.openid = response.data.data.openid
			// 						uni.setStorageSync('openid', that.openid)

			// 					}

			// 				}
			// 			})

			// 		},
			// 		fail: function(err) {
			// 			console.log(err)
			// 			// 登录授权失败
			// 			// err.code是错误码
			// 		}
			// 	})

			// },
			checkOpenid() {
				uni.showLoading({
					title: '查询中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/checkOpenid',
					method: 'POST',
					dataType: 'json',
					data: {
						PERTB005CA11: this.openid,
						token: this.token
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							console.log(res)
							var str = JSON.stringify(res.data.data)
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})

							uni.setStorageSync('userinfo', str);
							uni.setStorageSync('token', this.token);

							uni.switchTab({
								url: '/pages/tabbar/home/index'
							});

						}
						if (res.data.code == 500) {
							console.log(res)

							uni.showModal({
								title: '提示',
								content: res.data.message,
								cancelText: "取消", // 取消按钮的文字  
								confirmText: "确认", // 确认按钮的文字  
								showCancel: true, // 是否显示取消按钮，默认为 true
								confirmColor: '#f55850',
								cancelColor: '#39B54A',
								success: (res) => {
									if (res.confirm) {
										// console.log('comfirm') //点击确定之后执行的代码

										// this.hqwxuse()
										let nickName = ''
										uni.navigateTo({
											url: '/pages/daiOrde/bindingPhone/index?nickName=' +
												encodeURIComponent(nickName)
										})

									} else {
										console.log('cancel') //点击取消之后执行的代码
									}
								}
							})


						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
					}
				});
			},
			getCode() {
				if (this.timeOut != 60) return
				var reg = /^1[3-9][0-9]{9}$/;
				if (!reg.test(this.iphone)) {
					uni.showToast({
						title: '请输入有效手机号',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				uni.showLoading({
					title: '发送中',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/getCode',
					method: 'POST',
					dataType: 'json',
					data: {
						PERTB006CA01: this.iphone
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							uni.showToast({
								title: '发送成功',
								duration: 5000,
								icon: 'none',
							});
							this.countdown()
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
						uni.hideLoading();
					},
					fail: (res) => {
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
						uni.hideLoading();
					}
				});
			},
			countdown() {
				var time = setInterval(() => {
					if (this.timeOut == 0) {
						clearInterval(time)
						this.codetext = '获取验证码'
						this.timeOut = 60
					} else {
						var outTime = this.timeOut--
						if (this.timeOut < 10) {
							outTime = '0' + outTime
						}
						this.codetext = outTime + '秒后重新获取'
					}
				}, 1000)
			},
			getToken() {
				uni.request({
					url: this.baseUrl + '/system/token',
					method: 'GET',
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							this.token = res.data.data
						}
					},
					fail: (res) => {
						console.log(res);
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
					}
				});
			},
			submitLogin() {
				// console.log(uni.getStorageSync('cid'))
				var reg = /^1[3-9][0-9]{9}$/;
				if (!reg.test(this.iphone)) {
					uni.showToast({
						title: '请输入有效手机号',
						duration: 5000,
						icon: 'none',
					});
					return
				}
				if (!this.code && (this.code.length != 4 || this.code.length != 6)) {
					uni.showToast({
						title: '请输入有效验证码',
						duration: 5000,
						icon: 'none',
					});
					return
				}

				if (this.selectedValues.length == 0) {
					return this.$refs.uToast.show({
						title: '请勾选协议',
						type: 'default',
						icon: false
					})
				}
				uni.showLoading({
					title: '登录中...',
					mask: true
				});
				uni.request({
					url: this.baseUrl + '/system/Login',
					method: 'POST',
					dataType: 'json',
					data: {
						PERTB005CA03: this.iphone,
						ORMTB400CA03: this.erpID,
						ORMTB410CA00: '',
						PERTB006CA00: this.code,
						PERTB007CA00: this.yqm,
						token: this.token,
						cid: this.cid,
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading();
						if (res.data.code == 200) {
							var str = JSON.stringify(res.data.data)
							uni.setStorageSync('userinfo', str);
							uni.setStorageSync('token', this.token);
							if (this.ErpUserType == 'HF') {
								uni.switchTab({
									url: '/pages/tabbar/home/index'
								});
							} else if (this.ErpUserType == 'FP') {
								uni.switchTab({
									url: '/pages/tabbar/homeFP/index'
								});
							}

						} else if (res.data.code == 300) {
							// uni.reLaunch({
							// 	url: '../registered/index?iphone=' + this.iphone + '&yqm=' + this.yqm +
							// 		'&token=' + this
							// 		.token
							// })
							uni.switchTab({
								url: '/pages/tabbar/home/index'
							});
						} else {
							this.$refs.uToast.show({
								title: res.data.message,
								type: 'default',
								icon: false
							})
						}
					},
					fail: (res) => {
						uni.hideLoading();
						this.$refs.uToast.show({
							title: '服务器连接超时',
							type: 'default',
							icon: false
						})
					}
				});
			},
			iphoneIpt() {
				var regP = /^1[3-9][0-9]{9}$/;
				if (regP.test(this.iphone)) {
					uni.request({
						url: this.baseUrl + '/system/checkPhone/' + this.iphone,
						method: 'GET',
						success: (res) => {
							console.log(res)
							if (res.data.code == 201) {
								uni.reLaunch({
									url: '/page/registered/index?iphone=' + this.iphone + '&token=' +
										this.token
								})
							} else if (res.data.code != 200 && res.data.code != 201) {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'default',
									icon: false
								})

							}
						},
						fail: (res) => {
							uni.hideLoading();
							this.$refs.uToast.show({
								title: '服务器连接超时',
								type: 'default',
								icon: false
							})
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.checkbox {
		display: flex;
		align-items: center;
		margin: 20rpx 10rpx 0;
		font-size: 30rpx;
		width: 80%;
		margin: 0 auto;

		text {
			color: #00A2F2;
		}
	}

	.login-top {
		width: 45%;
		margin: 160rpx auto 40rpx auto;
	}

	.login-top-image {
		width: 200rpx;
		height: 200rpx;
		margin: 0rpx auto;
	}

	.login-top-image image {
		width: 200rpx;
		height: 200rpx;
	}

	.login-top-title {
		margin-top: 20rpx;
		font-size: 32px;
		font-family: Source Han Sans SC;
		font-weight: 500;
		color: #00A2F2;
		text-align: center;
	}

	.login-top-title-text {
		margin-top: 10rpx;
		font-size: 28rpx;
		font-family: Source Han Sans SC;
		color: #666666;
		text-align: center;
	}

	.login-content {
		padding: 60rpx 60rpx 0;
	}

	.login-li {
		display: flex;
		border-bottom: 1px solid #e3e3e3;
		margin-top: 20rpx;
		font-size: 26rpx;
		padding-left: 20rpx;
	}

	.login-li text {
		width: 40rpx;
		height: 60rpx;
		line-height: 60rpx;
	}

	.login-li input {
		flex: 1;
		height: 60rpx;
		line-height: 60rpx;
	}

	.login-li view {
		text-align: center;
		width: 260rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 24rpx;
		color: rgba(0, 168, 242, 1);
	}

	.login-li+.login-li {
		margin-top: 40rpx;
	}

	.login-li .iconfont {
		color: rgba(178, 178, 178, 1)
	}

	.login-content-btn {
		background-color: rgba(0, 122, 255, 1);
		color: #FFF;
	}

	.login-content-btn-active {
		background-color: rgba(0, 122, 255, .5);
	}

	.login-button {
		margin: 170rpx 40rpx 0;
	}

	.atdlfs-main-sty {
		text-align: center;
		padding: 60rpx 0;
		color: #666666;
	}

	.sjhyjdl-sty {
		width: 330rpx;
		margin: 0 auto;
	}
</style>